package com.ht.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.ht.data.Result;
import com.ht.domin.bo.ArticleBO;
import com.ht.domin.po.Article;
import com.ht.service.RecommendedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class RecommendedServiceImpl implements RecommendedService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Result findArticle() {
        try {
            List<Article> articleList = mongoTemplate.findAll(Article.class);
            return Result.OK().setData(articleList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.ERROR();
    }

    @Override
    public Result insert(Article article) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date());
            article.setId(RandomUtil.randomString(10));
            article.setPublishedTime(date);
            article.setArticleStatus("0");
            mongoTemplate.insert(article);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.ERROR();
    }
}
